板载传感¶
传感模块:按钮、触摸传感器、红外接近传感器、光线传感器、声音传感器、板载温度和CPU温度、加速度、RTC时钟等
在传感器部分,我们将一些最常见的传感器相关指令进行封装,这大大节省了学生的编程时间,这将更有助于使学生将更多的精力集中在创意实践上。
1.按钮¶
1 2 3 4 5 6 | from button import button_B1
button_B1.is_pressed()
button_B1.was_pressed()
button_B1.get_presses(1)
|
1.1 描述¶
按钮状态监测的三种情况:①按钮被按着?只要按着则返回True,一直按着则一直返回True,不按就返回False;②按钮被按下?是一个完整动作,也就是按下后被检测到了返回True,若不松开,则在此期间返回None,仅当松开后返回一次False,之后一直返回None;③至于按钮在某个时长内被按下的次数,某个时间段内一次不按则返回0,按几次返回数值几。
1.2 范例¶
通过串口打印输出三种按钮检测情况的返回值(若实际测试中看着不方便,可以逐一测试)。
如:
源代码:
1 2 3 4 5 6 7 8 9 10 11 import time from button import button_B1 from button import button_B2 from button import button_A1 while True: time.sleep(1) print('被按下返回:', button_B1.was_pressed()) print('被按着返回:', button_B2.is_pressed()) print('被按下的次数返回:', button_A1.get_presses(1))
2.触摸传感器¶
1 2 3 4 | from touchpad import touch_T1
touch_T1.is_touched()
|
2.1 描述¶
MixGo CE板载4个触摸按键,返回值为布尔值,即当触摸按键时返回True,否则返回False。
3.红外接近传感器¶
1 2 3 4 | from infrared import near
near("left")
|
3.1 描述¶
MixGo CE 3.0版本以上,带有两对红外接近传感器,用于检测是否有人体靠近;返回值为布尔值,当人体靠近传感器时返回True,否则返回为False。
3.2范例¶
通过串口返回左侧的红外接近传感器的状态。
4.光线传感器¶
1 2 3 4 | import sensor
sensor.get_brightness()
|
4.1 描述¶
MixGo CE 板载光线传感器,用于检测当前所在环境的光照强度,返回值为模拟量,范围0-65535。
4.2范例¶
通过串口返回当前环境的光照强度值。
5.声音传感器¶
1 2 3 4 | import sensor
sensor.get_soundlevel()
|
5.1 描述¶
MixGo CE 板载声音传感器,用于检测当前所在环境的声音强度,返回值为模拟量,范围0-65535。
5.2范例¶
通过串口返回当前环境的声音强度值。
6.温度传感器¶
1 2 3 4 | import sensor
sensor.get_temperature()
|
6.1 描述¶
MixGo CE 板载温度传感器,用于检测当前所在环境的温度及CPU温度,CPU温度获取可通过点击该图形指令右下角三角按钮切换,返回值为模拟量,范围0-65535。
6.2范例¶
通过串口返回当前环境的温度值。
7.加速度传感器¶
1 2 3 4 | from mixgoce import acc
acc.acceleration[0]
|
7.1 描述¶
MixGo CE 板载三轴加速度传感器,用于检测控制板当前的状态,具体可细分为x轴、y轴、z轴,返回值为浮点数。
7.2范例¶
通过串口返回控制板x轴加速度值。
8.设置RTC时钟¶
1 2 3 4 5 6 7 | from mixgoce import rtc_clock
import time
rtc_clock.datetime = time.struct_time((2020,12,26,14,20,45,6,361,-1))
rtc_clock.datetime
|
8.1 描述¶
实时时钟的缩写是RTC(Real_Time Clock),其中time.struct_time((2020,12,26,14,20,45,6,361,-1))为初始化操作,之后通过rtc_clock.datetime指令获取当前时间。
8.2范例¶
通过串口每隔1秒打印一次当前时间。